<template>
  <div class="tag-manage-page">
    <div class="header">
      <h2>会话标签管理</h2>
      <el-button type="primary" @click="openEditDialog()">新增标签</el-button>
    </div>
    <el-card>
      <el-table :data="tags" style="width: 100%">
        <el-table-column prop="name" label="标签名称" min-width="120" />
        <el-table-column prop="color" label="颜色" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.color">{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteTag(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <el-dialog v-model="editDialog.visible" :title="editDialog.data ? '编辑标签' : '新增标签'" width="400px">
      <el-form :model="editDialog.form" label-width="60px">
        <el-form-item label="名称">
          <el-input v-model="editDialog.form.name" maxlength="10" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-select v-model="editDialog.form.color" placeholder="请选择颜色">
            <el-option label="蓝色" value="info" />
            <el-option label="绿色" value="success" />
            <el-option label="黄色" value="warning" />
            <el-option label="红色" value="danger" />
            <el-option label="灰色" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialog.visible = false">取消</el-button>
        <el-button type="primary" @click="saveTag">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const tags = ref([
  { id: 1, name: '投诉', color: 'danger' },
  { id: 2, name: '催单', color: 'warning' },
  { id: 3, name: '售后', color: 'info' },
  { id: 4, name: '高价值客户', color: 'success' }
])

const editDialog = ref({ visible: false, data: null as any, form: { name: '', color: '' } })
function openEditDialog(row?: any) {
  if (row) {
    editDialog.value.data = row
    editDialog.value.form = { name: row.name, color: row.color }
  } else {
    editDialog.value.data = null
    editDialog.value.form = { name: '', color: '' }
  }
  editDialog.value.visible = true
}
function saveTag() {
  const { name, color } = editDialog.value.form
  if (!name.trim()) {
    ElMessage.warning('请输入标签名称')
    return
  }
  if (editDialog.value.data) {
    editDialog.value.data.name = name
    editDialog.value.data.color = color
    ElMessage.success('编辑成功')
  } else {
    tags.value.push({ id: Date.now(), name, color })
    ElMessage.success('新增成功')
  }
  editDialog.value.visible = false
}
function deleteTag(row: any) {
  tags.value = tags.value.filter(t => t.id !== row.id)
  ElMessage.success('删除成功')
}
</script>

<style scoped>
.tag-manage-page {
  padding: 24px;
  background: #f7f8fa;
  min-height: 100vh;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.header h2 {
  margin: 0;
  color: #333;
}
</style> 